package org.jeecg.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jeecgframework.p3.core.util.HttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DouYinUtil {


    public static Map<String,String> getDouYinInfo () throws IOException {

        String url = "https://open.douyin.com/oauth/access_token";
        List<NameValuePair> nameValuePairList = new ArrayList<>();
        nameValuePairList.add(new BasicNameValuePair("client_secret", "client_secret"));
        nameValuePairList.add(new BasicNameValuePair("code", "code"));
        nameValuePairList.add(new BasicNameValuePair("grant_type", "authorization_code"));
        nameValuePairList.add(new BasicNameValuePair("client_key", "client_key"));
        String result = HttpUtils.postForm(url, nameValuePairList);
        JSONObject jsonObject = JSONObject.parseObject(result);

        JSONObject data = jsonObject.getJSONObject("data");
        Map<String,String> resultMap = new HashMap<>();
        resultMap.put("access_token",data.get("access_token").toString());
        resultMap.put("open_id",data.get("open_id").toString());
        return  resultMap;

    }

    public static Map<String,String> getDouYinInfo (String clientId,String clientSecret,String  code) throws IOException {

        String url = "https://open.douyin.com/oauth/access_token";
        List<NameValuePair> nameValuePairList = new ArrayList<>();
        nameValuePairList.add(new BasicNameValuePair("client_secret", clientSecret));
        nameValuePairList.add(new BasicNameValuePair("code", code));
        nameValuePairList.add(new BasicNameValuePair("grant_type", "authorization_code"));
        nameValuePairList.add(new BasicNameValuePair("client_key", clientId));
        String result = HttpUtils.postForm(url, nameValuePairList);
        JSONObject jsonObject = JSONObject.parseObject(result);

        JSONObject data = jsonObject.getJSONObject("data");
        Map<String,String> resultMap = new HashMap<>();
        resultMap.put("access_token",data.get("access_token").toString());
        resultMap.put("open_id",data.get("open_id").toString());
        return  resultMap;

    }



    public static JSONObject getDouYinInfo (String openId,String token) throws IOException {

        String url = "https://open.douyin.com/oauth/userinfo?open_id="+openId+"&access_token="+ token;
        String result = HttpUtils.get(url);
        JSONObject jsonObject = JSONObject.parseObject(result);

        JSONObject data = jsonObject.getJSONObject("data");

        return  data;

    }

    /**
     * 获取粉丝数量
     * @return
     * @throws IOException
     */

    public static JSONObject getFans () throws IOException {
        Map<String,String> paramMape =  getDouYinInfo ();

        String url = "https://open.douyin.com/data/external/user/fans?access_token="+ paramMape.get("access_token")+"&open_id="+paramMape.get("open_id")+"&date_type=1";;

        String result = HttpUtils.get(url );
        JSONObject jsonObject = JSONObject.parseObject(result);


        return  jsonObject;

    }


    /**
     * 获取点赞数
     * @return
     * @throws IOException
     */
    public static JSONObject getlike () throws IOException {
        Map<String,String> paramMape =  getDouYinInfo ();

        String url = "https://open.douyin.com/data/external/user/like?access_token="+ paramMape.get("access_token")+"&open_id="+paramMape.get("open_id")+"&date_type=1";

        String result = HttpUtils.get(url );
        JSONObject jsonObject = JSONObject.parseObject(result);


        return  jsonObject;

    }

    /**
     * 获取分享数
     * @return
     * @throws IOException
     */
    public static JSONObject getShare () throws IOException {
        Map<String,String> paramMape =  getDouYinInfo ();

        String url = "https://open.douyin.com/data/external/user/share?access_token="+ paramMape.get("access_token")+"&open_id="+paramMape.get("open_id")+"&date_type=1";;

        String result = HttpUtils.get(url );
        JSONObject jsonObject = JSONObject.parseObject(result);


        return  jsonObject;

    }
}
